#include <stdio.h>
__global__ void getID(int * threadID, int * blockID, int * blkDm, int * grdDm, int glbID, int * global)
{
int globalID = threadIdx.x+blockIdx.x*blockDim.x;
glbID = globalID;
threadID[globalID] = threadIdx.x;
blockID[globalID] = blockIdx.x;
blkDm[globalID] = threadIdx.y;
grdDm[globalID] = blockIdx.y;
global[globalID]=globalID;
}

int main(int argc, char **argv)
{
int i,N = 16;
size_t size = sizeof(int)*N;
int glID;
int* h_thrID = (int*)malloc(size); int* h_blkID = (int*)malloc(size); int* h_blkDm = (int*)malloc(size); 
int* h_grdDm = (int*)malloc(size); int* h_global  = (int*)malloc(size);

int* d_thrID=NULL; int* d_blkID=NULL; int* d_blkDm=NULL; int* d_grdDm=NULL; int* d_global=NULL;
cudaMalloc((void**)&d_thrID,size); cudaMalloc((void**)&d_blkID,size); cudaMalloc((void**)&d_blkDm,size); cudaMalloc((void**)&d_grdDm,size);
cudaMalloc((void**)&d_global,size);

dim3 numBlocks(2); dim3 threadsPerBlock(2,2);
printf("GRID: x=%d y=%d z=%d\n",numBlocks.x,numBlocks.y,numBlocks.z);
printf("BLCK: x=%d y=%d z=%d\n",threadsPerBlock.x,threadsPerBlock.y,threadsPerBlock.z);
getID<<<numBlocks,threadsPerBlock>>>(d_thrID,d_blkID,d_blkDm,d_grdDm,glID,d_global);
cudaMemcpy(h_thrID,d_thrID,size,cudaMemcpyDeviceToHost);
cudaMemcpy(h_blkID,d_blkID,size,cudaMemcpyDeviceToHost);
cudaMemcpy(h_blkDm,d_blkDm,size,cudaMemcpyDeviceToHost);
cudaMemcpy(h_grdDm,d_grdDm,size,cudaMemcpyDeviceToHost);
cudaMemcpy(h_global,d_global,size,cudaMemcpyDeviceToHost);
for(i=0;i<N;i++)
	printf("%d : glID=%d :threadID_x=%d : blkID_x=%d : thr_y=%d : blk_y=%d\n",i,h_global[i],h_thrID[i],h_blkID[i],h_blkDm[i],h_grdDm[i]);
return 0;
}
